package com.woniu.supermarket.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.supermarket.entity.Unit;
import com.woniu.supermarket.mapper.UnitMapper;
import com.woniu.supermarket.service.IUnitService;
import com.woniu.supermarket.util.CodeCreateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UnitServiceImpl implements IUnitService {
    @Autowired
    UnitMapper unitMapper;
    @Override
    public int remove(int id) {
        return unitMapper.deleteByPrimaryKey(id);
    }
    @Override

    public int add(Unit unit) {
        unitMapper.insert(unit);
        Integer id = unit.getId();
        unit.setUnitCode(CodeCreateUtil.createGoodsUnitCode(id));
        return unitMapper.updateByPrimaryKey(unit);
    }


    @Override
    public int modify(Unit unit) {
        return unitMapper.updateByPrimaryKey(unit);
    }
    @Override
//分页查询
    public PageInfo<Unit> queryByPage(Integer now, Integer size, String cons) {
        PageHelper.startPage(now,size);
        List<Unit> ulists = unitMapper.selectByCons(cons);
        return new PageInfo<>(ulists);
    }
    @Override
    public Unit queryById(int id) {
        return unitMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Unit> findAll() {
        return unitMapper.selectAll();
    }
}
